library(logr)
log_open("log_A6.log")
log_code()
####################
#REPLICATION FILES: APPENDIX 6
#Article: "When Does Online Public Diplomacy Succeed? Evidence from China's ‘Wolf Warrior’ Diplomats"
#Authors: Daniel Mattingly and James Sundquist
#This Version: June 18, 2022


####################
#Description:
# This script estimates Average Treatment Effects using Ordinary Least Squares for each
# outcome question in our survey. (Our main analysis focused on composite outcomes).
# It will produce the figures presented in Appendix 6 of the article.

#The script was written with R version 4.1.2
#It was created and tested on Mac OS X (11.6.5).

library(tidyverse)
library(xtable)
library(estimatr)
library(rio)
library(coefplot)


as.num <- function(x){
  as.numeric(as.character(x))
}

setwd("~/Dropbox/Research/Diplomacy/Replication")
#setwd("~/Dropbox/ResearchProjects/Diplomacy/Data/Lucid")

# load cleaned data
pooled <- import("twitter_diplomacy_data.csv")
# Ensure levels of treatment variable appear in proper order
pooled$Treatment <- factor(pooled$Treatment, levels = c("control", "prochina", "antius"))
pooled$t_ <- pooled$Treatment

pre <- pooled %>% filter(galwan == 0)
post <- pooled %>% filter(galwan == 1)


# Individual question plots
df <- pooled %>% select(12:42, 53, 57)
df <- df %>% gather(1:31, key = "Likert", value = "Y")
by_outcome <- df %>% group_by(Likert) %>% nest() # dataframe of dataframes

# model-fitting functions
Likert_model <- function(my_df) {
  lm(Y ~ t_, data = my_df)
}


# GROUP 1: China Government questions
c.gov <- by_outcome[4:8,] # which outcomes for this group
c.gov <- c.gov %>% mutate(model = map(data, Likert_model)) # fit model for each outcome
c.gov.ss <- c.gov %>% mutate(ss = map(model, broom::tidy)) %>%
  unnest(ss) # summary stats
c.gov.ss <- filter(c.gov.ss, term != "(Intercept)")


my.outcome.text <- c("Chinese government \nis trustworthy.",
                  "Chinese government \nis dangerous.",
                  "South China Sea",
                  "China's rise good \nfor India's economy",
                  "China's rise good \nfor India's nat. sec.")

coefmaker <- function(outcome.text, ss.df, title){
outcome <- rep(outcome.text, each = 2)
treat <- rep(c("Foreign Aid", "Wolf Warrior"), dim(ss.df)[1]/2)
estimate <- as.num(ss.df$estimate)
se <- as.num(ss.df$std.error)
lower <- estimate - 1.96*se
upper <- estimate + 1.96*se
df <- cbind.data.frame(outcome, treat, estimate, se, lower, upper) %>% as.data.frame()
df$outcome <- factor(df$outcome, levels = rev(outcome.text))
df$treat <- factor(df$treat, levels = c("Wolf Warrior", "Foreign Aid"))

fig1 <- ggplot(data = df, aes(x= estimate, xmin=lower, xmax = upper, y = outcome, shape = treat))
fig1 <- fig1 + geom_point(position = coefplot::position_dodgev(height=.4), size = 2)
fig1 <- fig1 + geom_errorbarh(position = coefplot::position_dodgev(height=.4), height = 0)
fig1 <- fig1 + geom_vline(xintercept = 0, linetype = "dashed") + theme_bw()
fig1 <- fig1 + scale_shape_manual(values = c(17, 16)) + guides(shape = guide_legend(reverse = TRUE))
fig1 <- fig1 + xlab("ATE estimate, raw scale (1-7)") + theme(panel.grid.major = element_blank(),
                                                          panel.grid.minor = element_blank(),
                                                          panel.background = element_blank(),
                                                          axis.title.y = element_blank(),
                                                          legend.title = element_blank())
fig1 + ggtitle(as.character(title))
}
pdf("FigureA8.pdf", width=4, height = 4)
coefmaker(my.outcome.text, c.gov.ss, "")
dev.off()


#GROUP 2: Chinese People and Culture questions
c.people <- by_outcome[1:3,] # which outcomes for this group
c.people <- c.people %>% mutate(model = map(data, Likert_model)) # fit model for each outcome
c.people.ss <- c.people %>% mutate(ss = map(model, broom::tidy)) %>%
  unnest(ss) # summary stats
c.people.ss <- filter(c.people.ss, term != "(Intercept)")

my.outcome.text <- c("Good that Chinese ideas and\n customs are spreading here.",
                     "Chinese culture has \npositive aspects.",
                     "People from China \nare trustworthy.")
pdf("FigureA9.pdf", width=4.5, height = 2.9)
coefmaker(my.outcome.text, c.people.ss, "")
dev.off()

#GROUP 3: India's policy toward China questions
c.policy <- by_outcome[9:14, ]
c.policy <- c.policy %>% mutate(model = map(data, Likert_model)) # fit model for each outcome
c.policy.ss <- c.policy %>% mutate(ss = map(model, broom::tidy)) %>%
  unnest(ss) # summary stats
c.policy.ss <- filter(c.policy.ss, term != "(Intercept)")

my.outcome.text <-  c("Cooperate more on trade.",
            "Cooperate more on defense.",
            "Condemn China re: Xinjiang",
            "Santion China re: Xinjiang",
            "Offer asylum to Uyghurs",
            "Choose China over US")
pdf("FigureA10.pdf", width = 4.5, height = 3)
coefmaker(my.outcome.text, c.policy.ss, "")
dev.off()

# GROUP 4: China's Response to Covid questions
c.covid <- by_outcome[15:17, ]
c.covid <- c.covid %>% mutate(model = map(data, Likert_model)) # fit model for each outcome
c.covid.ss <- c.covid %>% mutate(ss = map(model, broom::tidy)) %>%
  unnest(ss) # summary stats
c.covid.ss <- filter(c.covid.ss, term != "(Intercept)")

my.outcome.text <- c("Good job responding \nto epidemic.",
                     "China responsible \nfor COVID-19",
                     "China has been generous\n in helping other countries\n during epidemic.")
pdf("FigureA11.pdf", width=4.5, height = 2.9)
coefmaker(my.outcome.text, c.covid.ss, "")
dev.off()

log_close()

